package com.sskj.postar.service.impl;

import com.sskj.postar.core.config.ServiceException;
import com.sskj.postar.core.universal.AbstractService;
import com.sskj.postar.core.utils.NumberUtil;
import com.sskj.postar.mapper.UserAssetMapper;
import com.sskj.postar.model.UserAsset;
import com.sskj.postar.service.UserAssetService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
* @Description: UserAssetService接口实现类
*
* @date 2018/11/09 13:26
*/
@Service
public class UserAssetServiceImpl extends AbstractService<UserAsset> implements UserAssetService {

    @Resource
    private UserAssetMapper userAssetMapper;


    @Override
    public void updateMoney(int userId,String type,String availableAdd, String freezeAdd) {
        UserAsset userAssetQuery = new UserAsset();
        userAssetQuery.setType(type);
        userAssetQuery.setUserId(userId);
        UserAsset userAsset = selectOne(userAssetQuery);
        if (userAsset==null){
            userAssetQuery.setFreeze(freezeAdd);
            userAssetQuery.setAvailable(availableAdd);
            insert(userAssetQuery);
        }else {
            userAsset.setAvailable(NumberUtil.keepUsdt(new BigDecimal(userAsset.getAvailable()).add(new BigDecimal(availableAdd)).toPlainString()));
            userAsset.setFreeze(NumberUtil.keepUsdt(new BigDecimal(userAsset.getFreeze()).add(new BigDecimal(freezeAdd)).toPlainString()));
            update(userAsset);
        }
    }

    @Override
    public void updateMoney(int id, String available, String freeze) {
       if (userAssetMapper.updateMoney(id,available,freeze)<0){
           throw new ServiceException("用户资金不足");
       }
    }

}

